/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var aeRequest;
var action;
var ajxUrl= "";
var CateArr = new Array();
var id;
var question;
var answer;
var faqsId;
var IdArr = new Array();
var rowId;

function createArray() {
	jQuery("#tblCategory>tbody>tr>td:nth-child(2)").each(function() {
		CateArr.push(jQuery(this).html());
	});
}
function createArrayId() {
	jQuery("#tblLink>tbody>tr>td:nth-child(1)").each(function() {
		CateArr.push(jQuery(this).html());
	});
}
function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}
function updateFAQs(faqsId,question,answer,action){
    var url = '/Alluring_Decors-war/FAQServlet?id='+ faqsId +'&question=' +question + '&answer=' + answer +
                '&action=' + action;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function finishUpdate(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
            if (result == "success") {
                //alert("update/edit successful");
            }else{
                //alert("update/edit failed");
            }
        }
    }
}

function setDeleteFormPosition(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#deleteContainer").height();
	var popupWidth = jQuery("#deleteContainer").width();

	jQuery("#deleteContainer").css({
		"position" : "absolute",
		"top" : (windowHeight/2 - popupHeight/2)-50,
		"left" : (windowWidth/2 - popupWidth/2)+150
	});
}
jQuery(document).ready(function() {
    jQuery(".delForm").live('click',function(){
        id = this.id;
        rowId = jQuery(this).parent().parent().attr('id');
        faqsId = jQuery(this).parent().parent().find('td:eq(0)').html();
        question = jQuery(this).parent().parent().find('td:eq(1)').html();
        answer = jQuery(this).parent().parent().find('td:eq(2)').html();
        
        if(jQuery("#deleteContainer").is(":hidden")){
            setDeleteFormPosition();
            jQuery("#deleteContainer").slideDown("slow");
        }else{
            jQuery("#deleteContainer").slideUp("slow");
        }
    });

    jQuery("#noDel").live('click',function(){
        jQuery("#deleteContainer").hide("explode",1000);
    });

    jQuery("#yesDel").live('click',function(){
        id = this.id;
        updateFAQs(faqsId, question, answer,"delete");

        jQuery("#"+rowId).remove();

        jQuery("#deleteContainer").hide("explode",1000);
    });
});


